//
//  ObjectRes.m
//  CandyJump
//
//  Created by Bon Lam on 01/03/2011.
//  Copyright 2011 Peru Republic. All rights reserved.
//

#import "ObjectRes.h"

#import "Model.h"
#import "PhysicsObject.h"

#import "AnimationRes.h"

#import "PRFile.h"

//---------------------------------------------------------------------------------------------------------------------
#define OBJECT_RES_FILE_LIST						@"StaticData/Objects/objectList.xml"

#define OBJECT_RES_XML_ELEM_TAG_OBJECT				@"object"
#define OBJECT_RES_XML_ELEM_TAG_SPRITESHEET			@"sprite"
#define OBJECT_RES_XML_ELEM_TAG_ANIMATION			@"animationInfo"
#define OBJECT_RES_XML_ELEM_TAG_PHYSICS				@"physicsInfo"
#define OBJECT_RES_XML_ELEM_TAG_BASICS_SETTINGS		@"basicSettings"
#define OBJECT_RES_XML_ELEM_TAG_SETTINGS			@"settings"

#define OBJECT_RES_XML_ATTR_TAG_CLASS				@"class"
#define OBJECT_RES_XML_ATTR_TAG_NAME				@"name"
#define OBJECT_RES_XML_ATTR_TAG_FILE				@"file"

//=====================================================================================================================
@implementation ObjectRes

@synthesize objectListDictionary;

static ObjectRes *sharedRes = nil;

#pragma mark ObjectRes - Singleton
//---------------------------------------------------------------------------------------------------------------------
- (id)init
{
	self = [super init];
	if (self == nil)  {return self;}
	
	return self;
}

//---------------------------------------------------------------------------------------------------------------------
- (void)dealloc
{
	[objectListDictionary release]; objectListDictionary = nil;
	
	[super dealloc];
}

//---------------------------------------------------------------------------------------------------------------------
+ (id)allocWithZone:(NSZone *)zone
{
    return [[self sharedRes] retain];
}

//---------------------------------------------------------------------------------------------------------------------
- (id)copyWithZone:(NSZone *)zone
{
    return self;
}

//---------------------------------------------------------------------------------------------------------------------
- (id)retain 
{
    return self;
}

//---------------------------------------------------------------------------------------------------------------------
- (NSUInteger)retainCount
{
    return NSUIntegerMax;  //denotes an object that cannot be released
}

//---------------------------------------------------------------------------------------------------------------------
- (void)release
{
    //do nothing
}

//---------------------------------------------------------------------------------------------------------------------
- (id)autorelease
{
    return self;
}

#pragma mark ObjectRes - Load Data
//---------------------------------------------------------------------------------------------------------------------
+ (ObjectRes *)sharedRes
{
    if (sharedRes == nil) {
        sharedRes = [[super allocWithZone:NULL] init];
    }
    return sharedRes;
}

//---------------------------------------------------------------------------------------------------------------------
- (void)loadObjects
{
	if (objectListDictionary != nil) {
		[objectListDictionary release];
		objectListDictionary = nil;
	}
	objectListDictionary = [[NSMutableDictionary alloc] init];
	
	NSString* filePath = [PRFile getFilePath:OBJECT_RES_FILE_LIST];
	[self parseXMLFile:filePath];
}

#pragma mark NSXMLParserDelegate
//---------------------------------------------------------------------------------------------------------------------
- (BOOL)parseXMLFile:(NSString *)filename
{
	//  Load Object List XML file
	NSData *xmlData = [NSData dataWithContentsOfFile:filename];
	if (xmlData == nil)  {
		NSLog(@"[ObjectRes parseXMLFile:filename] Error - Unable to parse file: ""%@""", filename);
		return NO;
	}
	
	NSError* error = nil;
	[self parseXMLData:xmlData parseError:&error];
	
	return (error == nil);
}

//---------------------------------------------------------------------------------------------------------------------
- (void)parseXMLData:(NSData *)data parseError:(NSError **)error
{
	NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];
    [parser setDelegate:self];
    [parser setShouldProcessNamespaces:NO];
    [parser setShouldReportNamespacePrefixes:NO];
    [parser setShouldResolveExternalEntities:NO];
    [parser parse];
	
    NSError *parseError = [parser parserError];
    if (parseError && error)  {
        *error = parseError;
    }
    [parser release];
}

//---------------------------------------------------------------------------------------------------------------------
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
	if ([elementName isEqualToString:OBJECT_RES_XML_ELEM_TAG_OBJECT]) {
		
		NSString* className = [attributeDict valueForKey:OBJECT_RES_XML_ATTR_TAG_CLASS];
		Class modelClass = NSClassFromString(className);
		if(modelClass == nil) { 
			NSLog(@"[ObjectRes parser: didStartElement: namespaceURI: qualifiedName: attributes:] Error - Unable to find class: ""%@"" ", className);
			return;
		}
		
		NSString* objectName = [attributeDict valueForKey:OBJECT_RES_XML_ATTR_TAG_NAME];
		if(objectName == nil) {
			NSLog(@"[ObjectRes parser: didStartElement: namespaceURI: qualifiedName: attributes:] Error - Unable to read object name");
			return;
		}
		
		currentReadingModel = [[modelClass alloc] init];
		[objectListDictionary setObject:currentReadingModel forKey:objectName];
		
		return;
	}
	
	if ([elementName isEqualToString:OBJECT_RES_XML_ELEM_TAG_SPRITESHEET]) {
		
		if (currentReadingModel == nil) {return;}
		
		for (NSString* key in [attributeDict allKeys]) {
			id value = [attributeDict objectForKey:key];
			[currentReadingModel setValue:value forKey:key];
		}
		return;
	}
	
	if ([elementName isEqualToString:OBJECT_RES_XML_ELEM_TAG_ANIMATION]) {

		if (currentReadingModel == nil) {return;}
		NSString* animationName = [attributeDict valueForKey:OBJECT_RES_XML_ATTR_TAG_FILE];
		currentReadingModel.animationDictionary = [[AnimationRes sharedRes] loadAnimation:animationName];
		
		return;
	}
	
	if ([elementName isEqualToString:OBJECT_RES_XML_ELEM_TAG_PHYSICS]) {
		
		if (currentReadingModel == nil) {return;}
		
		//	Return if not of class PhysicsObject
		if ([currentReadingModel isKindOfClass:[PhysicsObject class]] == NO) {return;}
		
		for (NSString* key in [attributeDict allKeys]) {
			id value = [attributeDict objectForKey:key];
			[currentReadingModel setValue:value forKey:key];
		}
		return;
	}
	
	if ([elementName isEqualToString:OBJECT_RES_XML_ELEM_TAG_SETTINGS] ||
		[elementName isEqualToString:OBJECT_RES_XML_ELEM_TAG_BASICS_SETTINGS]) {
		
		if (currentReadingModel == nil) {return;}
		
		for (NSString* key in [attributeDict allKeys]) {
			id value = [attributeDict objectForKey:key];
			[currentReadingModel setValue:value forKey:key];
		}
		return;
	}
}

//---------------------------------------------------------------------------------------------------------------------
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
	NSString* nodeName = [elementName lowercaseString];
		
	if ([nodeName isEqualToString:OBJECT_RES_XML_ELEM_TAG_OBJECT]) {
		[currentReadingModel release];
		currentReadingModel = nil;
	}
}

//---------------------------------------------------------------------------------------------------------------------
- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError
{
	
}

//---------------------------------------------------------------------------------------------------------------------
- (void)parserDidEndDocument:(NSXMLParser *)parser
{
	
}

@end
